function Line() {
	this.getAllBreakLatLng = function(path) {
		var lineDiff = 0.01;
		path = this.getAllBreakPoints(path, lineDiff);
		for (var i = 0; i < path.length; i++) {
			path[i] = new google.maps.LatLng(path[i][0], path[i][1]);
		}
		return path;
	};

	this.getAllBreakPoints = function(path, lineDiff) {
		var newPath = [ [ path[0].lat(), path[0].lng() ] ];
		for (var i = 0; i < path.length - 1; i++) {
			var a = [ path[i].lat(), path[i].lng() ];
			var b = [ path[i + 1].lat(), path[i + 1].lng() ];
			Array.prototype.push.apply(newPath, this.getAllBreakPointsBetween(
					a, b, lineDiff));
		}
		return newPath;
	};

	this.getAllBreakPointsBetween = function(a, b, lineDiff) {
		var points = [];
		var s1 = getXGridIntersections(a, b, lineDiff).sort(pointSorter);
		var s2 = getYGridIntersections(a, b, lineDiff).sort(pointSorter);
		var merged = merge(s1, s2);
		Array.prototype.push.apply(points, merged);
		points.push(b);
		return points;
	};
	
	function getYFromX(x1, x2, y1, y2, x) {
		if (x2 === x1)
			return y1;
		return ((y2 - y1) / (x2 - x1)) * (x - x1) + y1;
	}

	function getXFromY(x1, x2, y1, y2, y) {
		if (y2 === y1)
			return x1;
		return (y - y1) * (x2 - x1) / (y2 - y1) + x1;
	}

	function getGridLinesIntersection(p1, p2, lineDiff) {
		if (p2 < p1) {
			var t = p1;
			p1 = p2;
			p2 = t;
		}
		// p1 is smaller than p2
		var first = parseInt(p1 / lineDiff) * lineDiff;
		if (first < p1) {
			first += lineDiff;// for +sve numbers
		}
		var points = [];
		for (var i = first; i < p2; i += lineDiff) {
			points.push(i);
		}

		return points;
	}

	function getXGridIntersections(a, b, lineDiff) {
		var xpoints = getGridLinesIntersection(a[0], b[0], lineDiff);
		var linePoints = [];
		for (var i = 0; i < xpoints.length; i++) {
			var x = xpoints[i];
			var y = getYFromX(a[0], b[0], a[1], b[1], xpoints[i]);
			var d = distanceBetweenPoints(a, [ x, y ]);
			linePoints.push([ x, y, d ]);
		}
		return linePoints;
	}

	function getYGridIntersections(a, b, lineDiff) {
		var ypoints = getGridLinesIntersection(a[1], b[1], lineDiff);
		var linePoints = [];

		for (var i = 0; i < ypoints.length; i++) {
			var y = ypoints[i];
			var x = getXFromY(a[0], b[0], a[1], b[1], y);
			var d = distanceBetweenPoints(a, [ x, y ]);
			linePoints.push([ x, y, d ]);
		}
		return linePoints;

	}

	function distanceBetweenPoints(a, p) {
		return (p[0] - a[0]) * (p[0] - a[0]) + (p[1] - a[1]) * (p[1] - a[1]);
	}

	// var a = [ 4.4, 8.8 ]
	// var b = [ 1.1, 2.2 ]

	function pointSorter(a, b) {
		return a[2] > b[2];
	}

	// console.log(getXGridIntersections(a, b, 1).sort(pointSorter))
	// console.log(getYGridIntersections(a, b, 1).sort(pointSorter))

	function merge(a, b) {
		var c = [];
		var i = 0, j = 0;
		while (i < a.length && j < b.length) {
			if (Math.abs(a[i][2] - b[j][2]) < 0.000000001) {
				c.push([ a[i][0], a[i][1] ]);
				i++;
				j++;
			} else if (a[i][2] < b[j][2]) {
				c.push([ a[i][0], a[i][1] ]);
				i++;
			} else if (a[i][2] > b[j][2]) {
				c.push([ b[j][0], b[j][1] ]);
				j++;
			}
		}

		while (i < a.length) {
			c.push([ a[i][0], a[i][1] ]);
			i++;
		}

		while (j < b.length) {
			c.push([ b[j][0], b[j][1] ]);
			j++;
		}

		return c;
	}
}